package com.itheima.servlets.servlet;

import com.itheima.servlets.domain.Student;
import com.itheima.servlets.domain.User;
import com.itheima.servlets.service.UserService;
import com.itheima.servlets.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {
    //登录功能
    private void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //1.获取前端页面的用户名和密码
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //2.用户名和密码交由service处理，获取返回数据(user对象)
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, pwd);
        //3.判断输入账户与用户账户是否相同 相同则登陆成功
        if (user.getUsername().equals(username) && user.getPwd().equals(pwd) && user != null) {
            //登录成功 将登录者的信息保存到session中
            req.getSession().setAttribute("user", user);

            //获取复选框的值：是1表示记住；不是1表示不记住
            String ck = req.getParameter("ck");
            //实现记住状态操作
            //创建cookie对象
            Cookie cookieName = new Cookie("username", user.getUsername());
            Cookie cookiePwd = new Cookie("pwd", user.getPwd());
            if ("1".equals(ck)) {
                //设置有效期
                cookieName.setMaxAge(3600);
                cookiePwd.setMaxAge(3600);
            } else {
                //设置有效期
                cookieName.setMaxAge(0);
                cookiePwd.setMaxAge(0);
            }
            //设置存储路径
            cookieName.setPath("/");
            cookiePwd.setPath("/");

            //添加到response对象中
            resp.addCookie(cookieName);
            resp.addCookie(cookiePwd);

            //保存后 跳转主页面
            //resp.sendRedirect(req.getContextPath() + "/index.jsp");
            //写出数据
            resp.getWriter().write("登录成功！3秒后将跳转到初始页面！");
            //设置定时刷新 并跳转到初始页面
            resp.setHeader("Refresh", "3;URL=/day08_web_student/index.jsp");
        } else {
            //登录失败 返回登录页面
            //resp.sendRedirect(req.getContextPath() + "/login.jsp");
            //写出数据
            resp.getWriter().write("登录失败！3秒后将跳转到登录页面！");
            //设置定时刷新 并跳转到初始页面
            resp.setHeader("Refresh", "3;URL=/day08_web_student/login.jsp");
        }
    }

    //添加学生信息
    private void addStudent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取表单数据
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String math = req.getParameter("math");

        //创建学生对象并赋值
        Student stu = new Student();
        stu.setName(name);
        stu.setAge(Integer.parseInt(age));
        stu.setMath(Integer.parseInt(math));

        //学生信息交由service处理
        UserService userService = new UserServiceImpl();
        //调用添加方法
        userService.addStudent(stu);
        //通过定时刷新功能响应给浏览器
        //写出数据
        resp.getWriter().write("添加成功！3秒后将跳转到初始页面！");
        //设置定时刷新 并跳转到初始页面
        resp.setHeader("Refresh","3;URL=/day08_web_student/index.jsp");
    }

    //查看学生信息
    private void findStudent(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        //创建list集合
        ArrayList<Student> list = new ArrayList<>();
        //学生信息交由service处理
        UserService userService = new UserServiceImpl();
        //调用查看方法
        userService.findStudent(list);
        //将集合对象存入会话域中
        req.getSession().setAttribute("students",list);
        //重定向到学生列表页面
        resp.sendRedirect("/day08_web_student/list.jsp");
    }
}
